package com.edu.test;

import com.edu.bean.User;
import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
 * @Author:mx
 * @Date:2025/11/4-16:22:55
 */
public class MyTest05 {

    /*
    *   获取 一个 class文件 内部 包含的构造方法的的信息
    *       使用 Constructor 类的对象 来描述一个 构造方法的信息
    * */
    @Test
    public void test01() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class clazz = User.class;
//        Constructor[] constructors = clazz.getConstructors();
//        for (Constructor constructor : constructors) {
//            System.out.println(constructor.getName());
//            System.out.println(constructor.getParameterCount());
//            for (Class parameterType : constructor.getParameterTypes()) {
//                System.out.println(parameterType.getName());
//            }
//        }

//        for (Constructor constructor : clazz.getDeclaredConstructors()) {
//            System.out.println(constructor.getName());
//            System.out.println(constructor.getParameterCount());
//            for (Class parameterType : constructor.getParameterTypes()) {
//                System.out.println(parameterType.getName());
//            }
//        }

        Constructor constructor = clazz.getConstructor(int.class, String.class, String.class, int.class);

        Constructor constructor1 = clazz.getDeclaredConstructor(int.class);

        Object o = constructor.newInstance(1, "zhangsan", "123456", 18);
        System.out.println(o);

        constructor1.setAccessible(true);
        Object o1 = constructor1.newInstance(1);
        System.out.println(o1);
    }

    /*
    *   Method 的一些常用方法
    *
    *
    * */
    @Test
    public void test02() throws NoSuchMethodException {
        Class clazz = User.class;
        for (Method method : clazz.getDeclaredMethods()) {
            System.out.println("方法名:"+method.getName());
            System.out.println("参数列表");
            for (Class<?> parameterType : method.getParameterTypes()) {
                System.out.println(parameterType.getName());
            }
            System.out.println("返回值类型:"+method.getReturnType());
            System.out.println("修饰符:"+method.getModifiers());
            System.out.println("-----------");
        }
    }


}
